package com.yjf.ljr.activity;

import com.yjf.ljr.R;
import com.yjf.ljr.activity.IjActivity.ID;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.dataclass.AcountManageDataClass;
import com.yjf.ljr.dataclass.AcountManageDataClass.AcountManageDataInfo;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.SPreferences;

import android.R.bool;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AcountManageActivity extends IjActivity implements OnClickListener {

	@ID("rlrealNameInformation")
	private RelativeLayout mRlrealNameInformation;// 实名信息
	@ID("rlmobile")
	private RelativeLayout mRlmobile; // 手机号
	@ID("rlemail")
	private RelativeLayout mRlemail;// 电子邮箱
	@ID("rlpayment")
	private RelativeLayout mRlpayment;// 支付账户
	@ID("rlpassword")
	private RelativeLayout mRlpassword;// 登录密码
	@ID("rlgesture")
	private RelativeLayout mRlgesture;// 手势密码
	@ID("ivgestureDescript")
	private TextView mRlgestureDescript;// 手势密码描述
	@ID("tvRealNameAuthentication")
	private TextView mTvRealNameAuthentication;// 实名信息认证状态
	@ID("tvPswState")
	private TextView mTvPswState;// 登陆密码状态
	@ID("tvOpenState")
	private TextView mTvOpenState;// 开通状态

	@ID("mobile_status")
	private TextView mobileStatus;// 手机状态
	@ID("mail_status")
	private TextView mailStatus;
	
	@ID("ivarrowRightRealInfo")
	private ImageView ivarrowRightRealInfo;

	private String realName;// 真实姓名
	private String certNo;// 身份证号
	private String mobile;// 手机号码 不展示
	private String viewMobile;// 手机号码 展示用
	private String mail;// 电子邮箱
	private String accountName;// 支付账号
	private String realNameAuthentication;// 认证状态
	private String paymentOpenStatus = "";// 支付账户开通状态
	public static boolean FLAG_REFRESH = false;// 全局变量，修改手机号成功后刷新使用。
	private static final int MAIL_RESULT = 1004;// 修改邮箱请求码
	private String modifyYjfPwdUrl;// 修改易极付密码
	
	private String modifyMobileUrl;//富友修改手机号跳转链接
	
	private String activeYjfPwdUrl;//激活账户

	private static final int REQUEST_UPDATE_PWD = 1006;// 修改密码请求码

	private static final int REALNAME_RESULT = 1005;// 实名认证

	private static final int ACTIVE_PAYMENT = 1007;

	private static final int BIND_PHONE_NUMBER = 1008;// 绑定手机
	private final static String PATTERN_EVER_CHOSEN_KEY = "lockscreen.patterneverchosen";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_acountmanage);
		initControl();
	}

	private void initControl() {

		setLeftBtnClick();
		setTitleStr("账户管理");
		mRlrealNameInformation.setOnClickListener(this);
		mRlmobile.setOnClickListener(this);
		mRlemail.setOnClickListener(this);
		mRlpayment.setOnClickListener(this);
		mRlpassword.setOnClickListener(this);
		mRlgesture.setOnClickListener(this);
		showProgressDialog();
		new AcountManageTask().execute();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (FLAG_REFRESH) {
			FLAG_REFRESH = false;
			showProgressDialog();
			new AcountManageTask().execute();
		}
		String descript;
		boolean isLockPatternOpen = (Boolean) SPreferences.getData(this, false, CommonData.USER_NAME
				+ LockPatternSettingActivity.LOCK_PATTERN_IS_OPEN);
		boolean isFirstLogin = (Boolean) SPreferences.getData(mContext, false, CommonData.USER_NAME
				+ PATTERN_EVER_CHOSEN_KEY);
		if (!isFirstLogin) {
			descript = "未设置";
		} else {
			descript = isLockPatternOpen ? "已开启" : "已关闭";
		}
		mRlgestureDescript.setText(descript);
	}

	class AcountManageTask extends AsyncTask<Void, Void, String> {
		private String mFailStr;
		private AcountManageDataClass dc = new AcountManageDataClass();

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			RequestObject mObject = new RequestObject();
			mObject.method = "appAccountInfo";
			return requestDataAndGetErrorMsg(mObject, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			dismissProgressDialog();
			if ("-1".equals(dc.code)) {
				loginWebview();
				return;
			}
			if (!TextUtils.isEmpty(result)) {
				showToast(result);
			} else {
				AcountManageDataInfo data = dc.accountInfo;
				modifyYjfPwdUrl = data.modifyYjfPwdUrl;
				activeYjfPwdUrl = dc.activeAccountUrl;
				modifyMobileUrl = data.modifyMobileUrl;
				if (!TextUtils.isEmpty(data.realName)) {
					realName = data.realName;// 真实姓名
				}
				if (!TextUtils.isEmpty(data.certNo)) {
					certNo = data.certNo;// 身份证号
				}
				if (!TextUtils.isEmpty(data.mobile)) {
					mobile = data.mobile;// 手机号码 不展示
				}
				if (!TextUtils.isEmpty(data.viewMobile)) {
					viewMobile = data.viewMobile;// 手机号码 展示用
				}
				if (!TextUtils.isEmpty(data.mail)) {
					mail = data.mail;// 电子邮箱
					mailStatus.setText(getString(R.string.mobile_status_on));
				} else {
					mailStatus.setText(getString(R.string.mobile_status_off));
				}
				if (!TextUtils.isEmpty(data.realNameAuthentication)) {
					realNameAuthentication = data.realNameAuthentication;// 实名认证
				}
				if (!TextUtils.isEmpty(data.accountName)) {
					accountName = data.accountName;// 支付账号
				}
				if("IS".equalsIgnoreCase(data.realNameAuthentication)) {
					mTvRealNameAuthentication.setText("已认证");
					ivarrowRightRealInfo.setVisibility(View.INVISIBLE);
				} else {
					mTvRealNameAuthentication.setText("未认证");
					ivarrowRightRealInfo.setVisibility(View.VISIBLE);
				}

				// if (!TextUtils.isEmpty(data.mailBinding) &&
				// data.mailBinding.equals("IS")) {
				// mailStatus.setText(getString(R.string.mobile_status_on));
				// } else {
				// mailStatus.setText(getString(R.string.mobile_status_off));
				// }

				if (!TextUtils.isEmpty(data.mobileBinding) && data.mobileBinding.equals("IS")) {
					mobileStatus.setText(getString(R.string.mobile_status_on));
				} else {
					mobileStatus.setText(getString(R.string.mobile_status_off));
				}
				if (!TextUtils.isEmpty(data.openState)) {
					mTvOpenState.setText(data.openState);
					paymentOpenStatus = data.openState;
				}
				if (!TextUtils.isEmpty(data.pswState)) {
					mTvPswState.setText(data.pswState);
				}

			}
		}
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.rlrealNameInformation:
			if (mTvRealNameAuthentication.getText().toString().indexOf("已认证") != -1) {
				return;
			} else {
				Intent intent = new Intent(AcountManageActivity.this,RegistrationActiveActivity.class);
				intent.putExtra("phoneNum", mobile);
				startActivityForResult(intent, REALNAME_RESULT);			
			}
			break;
		case R.id.rlmobile:
			Intent intentMobile = new Intent(mContext, MobileDetailActivity.class);
			intentMobile.putExtra("mobile", mobile);
			intentMobile.putExtra("viewMobile", viewMobile);
			intentMobile.putExtra("modifyMobileUrl", modifyMobileUrl);
			startActivityForResult(intentMobile, BIND_PHONE_NUMBER);// 手机绑定
			break;
		case R.id.rlemail:
			Intent intentMail = new Intent(mContext, EmailDeatailActivity.class);
			intentMail.putExtra("mobile", mobile);
			intentMail.putExtra("viewMobile", viewMobile);
			intentMail.putExtra("mail", mail);
			startActivityForResult(intentMail, MAIL_RESULT);// 电子邮箱
			// startActivity(intentMail);// 电子邮箱
			break;
		case R.id.rlpayment:
			Intent intentPayment = new Intent(AcountManageActivity.this, PaymentActivity.class);
			intentPayment.putExtra("paymentStatus", paymentOpenStatus);
			intentPayment.putExtra("realName", accountName/*realName*/);
			intentPayment.putExtra("mobile", mobile);
			startActivityForResult(intentPayment,ACTIVE_PAYMENT);// 支付账户
			break;
		case R.id.rlpassword:
			// startActivity(new Intent(AcountManageActivity.this,
			// UpdatePwdActivity.class));// 修改登录密码 非网页
			Intent intentPwd = new Intent();
			// intentPwd.setClass(this, WvUpdatePwdActivity.class);
			// intentPwd.putExtra("advUrl", modifyYjfPwdUrl);
			// intentPwd.putExtra("advTitle", "修改密码");
			// intentPwd.putExtra("registration", "注册");
			intentPwd.setClass(AcountManageActivity.this, UpdatePwdActivity.class);
			startActivityForResult(intentPwd, REQUEST_UPDATE_PWD);
			break;
		case R.id.rlgesture:
			if (CommonData.IS_LOGIN) {
				startActivity(new Intent(AcountManageActivity.this, LockPatternSettingActivity.class));// 手势密码
			} else {
				loginWebview();
			}
			break;

		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, intent);
		switch (requestCode) {
		case MAIL_RESULT:

			if (-1 == resultCode) {
				boolean changeFlag = intent.getBooleanExtra("mailFlaf", false);
				if (changeFlag) {
					showProgressDialog();
					new AcountManageTask().execute();
				}
			}
			break;
		case REQUEST_UPDATE_PWD:
			if (-1 == resultCode) {
				setResult(RESULT_OK);
				AcountManageActivity.this.finish();
			}
			break;
		case CommonData.REQUEST_CODE_LOGIN:
			if (resultCode == RESULT_OK && CommonData.FLAG_LOGIN_SERVER) {
				FLAG_REFRESH = true;
			}
			break;
		case REALNAME_RESULT:
			if (resultCode == RESULT_OK) {
				FLAG_REFRESH = true;
			}
			break;
		case ACTIVE_PAYMENT:
			if (resultCode == RESULT_OK) {
				showProgressDialog();
				new AcountManageTask().execute();
			}
			break;
		case BIND_PHONE_NUMBER:
			if (RESULT_OK == resultCode) {
				showProgressDialog();
				new AcountManageTask().execute();
			}
			break;

		default:
			break;
		}
	}

}
